$().ready(function() {

	$.ajax({
		type:'get',
		url: '/jzweb/detailsReport/selTopicReportList?name='+'',
		success : function(r) {
			//考生
			$('#certificateList').selectPage({
				showField: 'topicName',
				keyField: 'id',
				data: r.data,
				pageSize:20,
				orderBy: ['id desc'],
				//向服务端提交的参数中，增加已选中的
				//设置返回-1，意为通知服务端返回空列表，初始化时使用
				params: function(){
					var pv = $('#certificateList').val();
					return {'cbatchId': pv?pv:''};
				},
				//格式化显示项目，提供源数据进行使用
				formatItem : function(data){
					return data.topicName;
				},
				eSelect : function(data){
					$('#certificateList').val(data.id);
				},
				eAjaxSuccess : function(d){
					var result;
					if(d) result = d.data;
					else result = undefined;
					return result;
				}
			});
		}
	});

	validateRule();
});

let type = $("#type").val();

switch (type){
	case "1": $("#video").remove();$("#picture").remove();$("#media").remove(); $("#videoMadio").remove(); break;
	case "2": $("#articleContent").remove();$("#picture").remove();$("#media").remove();  $("#videoMadio").remove();break;
	case "3": $("#articleContent").remove();$("#video").remove();$("#media").remove(); $("#videoMadio").remove();break;
	case "4": $("#articleContent").remove();$("#video").remove();$("#picture").remove(); $("#videoMadio").remove(); break;
	case "5": $("#articleContent").remove();$("#video").remove();$("#media").remove(); $("#videoMadio").remove();break;
	case "6": $("#articleContent").remove();$("#video").remove();$("#media").remove();break;
}


function save() {

	if( !$("#certificateList").val() ){
		layer.alert('专题为必填项');
		return false;
	}

	$("#article").val($('#content_sn').summernote('code'));
	$("#mediaRich").val($('#media_sn').summernote('code'));

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/detailsReport/saveDetailsReport",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}

//上传封面图
layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#business',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {

			let str = `
                <div class="closeBtn">
                    <img class="upImg" src="${r.data}" width="80" height="80"/>
                    <span class="iconclose"><img src="../../../img/close.png"></span>
                </div>
              `
			$("#license1").html(str)
			$("#videoCover").val(r.data);

			$(".iconclose").click(function (){
				$(this).parent('.closeBtn').remove();
				$("#license1").html('')
				$("#videoCover").val('')
			})

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

//上传媒体封面
layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#videoImg',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {

			let str = `
                <div class="closeBtn">
                    <img class="upImg" src="${r.data}" width="80" height="80"/>
                    <span class="iconclose"><img src="../../../img/close.png"></span>
                </div>
              `
			$("#license1s").html(str)
			$("#videoCovers").val(r.data);

			$(".iconclose").click(function (){
				$(this).parent('.closeBtn').remove();
				$("#license1s").html('')
				$("#videoCovers").val('')
			})

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

//上传视频
layui.use(['upload', 'element', 'layer'], function() {
	var $ = layui.jquery
		, upload = layui.upload,
		element = layui.element;

	upload.render({
		elem: '#videoSrc'
		, url: '/common/sysFile/upload'
		, accept: 'video' //视频
		,error: function(res){
			//演示失败状态，并实现重传
			if(res.code != 0){
				return layer.msg(res.msg);
			}
		}
		,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
			layer.load(3, {
				shade: [0.1, '#fff'],
				content: '<span class="loadtip">视频加载中~</span>',
				success: function (layer) {
					layer.find('.layui-layer-content').css({
						'padding-top': '30px',
						'width': '100px',
					});
					layer.find('.loadtip').css({
						'font-size':'14px',
						'margin-left':'5px'
					});
				}
			}); //上传loading
		}
		, done: function (res) {
			layer.closeAll('loading');
			if(res.code != 0){
				return layer.msg('上传失败');
			}

			$("#videoInfo").val(res.data);
			$("#videoShow").html(`
					<video width="200" height="200" src="${res.data}" controls autoplay>抱歉，您的浏览器不支持内嵌视频</video>
					<span class="iconclose"><img src="../../../img/close.png"></span>
				`);

			$(".iconclose").click(function (){
				$(this).parent('.closeBtn').remove();
				$("#videoShow").html('')
				$("#videoInfo").val('')
			})

		}
	});

})


$('#content_sn').summernote({
	height: '300px',
	lang: 'zh-CN',
	toolbar: [
		['style', ['bold', 'italic', 'underline', 'clear']],
		['font', ['strikethrough', 'superscript', 'subscript']],
		['fontsize', ['fontsize']],
		['fontname', ['fontname']],
		['color', ['color']],
		['para', ['ul', 'ol', 'paragraph']],
		['height', ['height']],
		['table', ['table']],
		['insert', ['link', 'picture']],
		['view', ['fullscreen', 'codeview', 'help']]
	],
	callbacks: {
		onImageUpload: function (files, editor, $editable) {
			sendFiles(files,'#content_sn');
		},
		onPaste: function (ne) {
			var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
			ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
			setTimeout(function () {
				document.execCommand("insertText", false, bufferText);
			}, 10);
		}
	}
});

$('#media_sn').summernote({
	height: '300px',
	lang: 'zh-CN',
	toolbar: [
		['style', ['bold', 'italic', 'underline', 'clear']],
		['font', ['strikethrough', 'superscript', 'subscript']],
		['fontsize', ['fontsize']],
		['fontname', ['fontname']],
		['color', ['color']],
		['para', ['ul', 'ol', 'paragraph']],
		['height', ['height']],
		['table', ['table']],
		['insert', ['link', 'picture']],
		['view', ['fullscreen', 'codeview', 'help']]
	],
	callbacks: {
		onImageUpload: function (files, editor, $editable) {
			sendFiles(files,'#media_sn');
		},
		onPaste: function (ne) {
			var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
			ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
			setTimeout(function () {
				document.execCommand("insertText", false, bufferText);
			}, 10);
		}
	}
});

//编辑器新增的ajax上传图片函数
function sendFiles(files, editor, $editable) {
	var formData = new FormData();
	formData.append("file", files[0]);
	$.ajax({
		data : formData,
		type : "POST",
		url : "/common/sysFile/upload",    // 图片上传出来的url，返回的是图片上传后的路径，http格式
		cache : false,
		contentType : false,
		processData : false,
		dataType : "json",
		success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名
			$(editor).summernote('insertImage',data.data);
		},
		error:function(data){
			//console.log("data",data)
			alert("上传失败");
		}
	});
}


$("#videoInfo").on('blur',function (val){
	if( val.target.value ){
		$("#videoShow").html(`
						<video width="200" height="200" src="${val.target.value}" controls autoplay>抱歉，您的浏览器不支持内嵌视频</video>
						<span class="iconclose"><img src="../../../img/close.png"></span>
					`);
	}else{
		$("#videoShow").html('');
	}

})